package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.SysUser;
import com.gx.mxbbx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @Autowired
    private UserService userservice;

    //用户信息列表
    @RequestMapping("/getUserList")
    public String getUserList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String userName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysUser> pageInfo = userservice.getUserList(userName, currentPage, limit);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", userName);

        return "/user/user_list";
    }

    //打开添加用户界面
    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        return "/user/user_add";
    }

    //添加用户
    @RequestMapping("/add")
    public String add(SysUser sysUser, HttpServletRequest request, Model model) {
        userservice.addUser(sysUser);
        return getUserList(1, 10, request, model);
    }

    //删除用户
    @RequestMapping("/delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        userservice.delete(id);
        return getUserList(1, 10, request, model);
    }

    //打开修改界面
    @RequestMapping("/openEdit")
    public String openEdit(Integer id, Model model) {
        SysUser sysUser = userservice.getUserById(id);
        model.addAttribute("user", sysUser);
        return "/user/user_edit";
    }

    //修改用户信息
    @RequestMapping("/edit")
    public String edit(SysUser sysUser, HttpServletRequest request, Model model) {
        userservice.edit(sysUser);
        return getUserList(1, 10, request, model);
    }

}
